package com.wiscamp.ninechapters.tags.infrastructure.mappers;

import com.wiscamp.ninechapters.tags.infrastructure.po.TagView;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Collection;

@Repository
public interface TagViewMapper extends JpaRepository<TagView, Long>, JpaSpecificationExecutor<TagView> {
    /**
     * 通过标签组编号和代码获取标签组列表
     *
     * @param tagCode
     * @return
     */
    @Query(value = "select e from TagView e where e.applicationId = :appId and e.tagCode = :tagCode")
    TagView findByTagCode(String tagCode, long appId);


    /**
     * 通过应用编号标签组名称和标签名称获取标签
     *
     * @param applicationId
     * @param groupName
     * @param tagName
     * @return
     */
    @Query(value = "select e from TagView e where e.applicationId = :appId and e.groupName = :groupName and e.tagName = :tagName")
    TagView findByName(String groupName, String tagName, long appId);

    /**
     * 分页获取标签列表
     *
     * @param spec
     * @param pageable
     * @return
     */
    Page<TagView> findAll(Specification<TagView> spec, Pageable pageable);


    @Query(value = "select e from TagView e where e.applicationId = :appId and e.groupName = :groupName")
    Collection<TagView> findListByGroupName(String groupName, long appId);

    /**
     * 通过tag编号列表获取tag名称列表
     *
     * @param tagIds
     * @return
     */
    @Query(value = "select e from TagView e where e.tagId in :tagIds")
    Collection<TagView> findListByTagIds(Collection<Long> tagIds);

    @Query(value = "select distinct e.groupName from TagView e where e.enabled")
    Collection<String> findAvailableGroups();
}